//
//  UserManager.m
//  HiZhua
//
//  Created by Jpwang on 2017/8/7.
//  Copyright © 2017年 Jpwang. All rights reserved.
//

#import "UserManager.h"

@implementation User

/* 获取对象的所有属性 以及属性值 */
- (NSDictionary *)properties {
    NSMutableDictionary *props = [NSMutableDictionary dictionary];
    unsigned int outCount, i;
    objc_property_t *properties = class_copyPropertyList([self class], &outCount);
    for (i = 0; i<outCount; i++)
    {
        objc_property_t property = properties[i];
        const char* char_f =property_getName(property);
        NSString *propertyName = [NSString stringWithUTF8String:char_f];
        id propertyValue = [self valueForKey:(NSString *)propertyName];
//        NSLog(@"propertyName=%@",propertyName);
//        NSLog(@"propertyValue=%@",propertyValue);
        if (propertyValue) [props setObject:propertyValue forKey:propertyName];
    }
    free(properties);
    return props;
}

@end


@implementation UserManager

+ (UserManager *)shareInstance {
    static UserManager *_sharedInstance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedInstance = [[UserManager alloc] init];
    });
    return _sharedInstance;
}

- (instancetype)init {
    self = [super init];
    if (self) {
        NSDictionary *temp = getValueByKey(@"user_login");
        if (temp) {
            [self setLoginMsg:temp];
        }
    }
    return self;
}

//- (void)setUserinfo:(NSDictionary *)userinfo {
//    if (userinfo == nil) {
//        self.user = nil;
//        saveValueByKey(nil, @"user_data");
//        return;
//    }
//    if (!self.user) {
//        self.user = [[User alloc] init];
//    }
//    if (isNil(userinfo[@"binded"])) {
//        self.user.binded = [userinfo[@"binded"] integerValue];
//    }
//    if (isNil(userinfo[@"expire"])) {
//        self.user.expire = [userinfo[@"expire"] integerValue];
//    }
//    if (isNil(userinfo[@"firstIn"])) {
//        self.user.firstIn = [userinfo[@"firstIn"] integerValue];
//    }
//    if (isNil(userinfo[@"imgUrl"])) {
//        if ([userinfo[@"imgUrl"] isKindOfClass:[NSString class]]) {
//            NSString *url = userinfo[@"imgUrl"];
//            if ([url hasPrefix:@"http"]) {
//                self.user.imgUrl = userinfo[@"imgUrl"];
//            }
//        }
//    }
//
//    if (isNil(userinfo[@"grabLevel"])) {
//        self.user.grabLevel = userinfo[@"grabLevel"];
//    }
//    if (isNil(userinfo[@"vipLevel"])) {
//        self.user.vipLevel = userinfo[@"vipLevel"];
//    }
//
//    if (isNil(userinfo[@"lastLoginTime"])) {
//        self.user.lastLoginTime = userinfo[@"lastLoginTime"];
//    }
//    if (isNil(userinfo[@"mcardGivePoint"])) {
//        self.user.mcardGivePoint = userinfo[@"mcardGivePoint"];
//    }
//    if (isNil(userinfo[@"mcardValidDays"])) {
//        self.user.mcardValidDays = userinfo[@"mcardValidDays"];
//    }
//
//    if (isNil(userinfo[@"nick"])) {
//        self.user.nick = userinfo[@"nick"];
//    }
//    if (isNil(userinfo[@"name"])) {
//        self.user.name = userinfo[@"name"];
//    }
//    if (isNil(userinfo[@"phone"])) {
//        self.user.phone = userinfo[@"phone"];
//    }
//    if (isNil(userinfo[@"refreshSession"])) {
//        self.user.refreshSession = userinfo[@"refreshSession"];
//    }
//    if (isNil(userinfo[@"session"])) {
//        id object = userinfo[@"session"];
//        @try {
//            if ([object isKindOfClass:[NSString class]]) {
//                self.user.session = userinfo[@"session"];
//            } else {
//                self.user.session = object[@"session"];
//                self.user.refreshExpire = object[@"refreshExpire"];
//                self.user.refreshSession = object[@"refreshSession"];
//                self.user.expire = [object[@"expire"] intValue];
//            }
//        } @catch (NSException *exception) {
//
//        }
//    }
//    if (isNil(userinfo[@"uid"])) {
//        if (self.user.uid == 0) {
//            self.user.uid = [userinfo[@"uid"] integerValue];
//        }
//    }
//    if (isNil(userinfo[@"userType"])) {
//        self.user.userType = [userinfo[@"userType"] integerValue];
//    }
//    if (isNil(userinfo[@"uuid"])) {
//        self.user.uuid = [userinfo[@"uuid"] integerValue];
//    }
//    if (isNil(userinfo[@"gender"])) {
//        self.user.gender = [userinfo[@"gender"] integerValue];
//    }
//    if (isNil(userinfo[@"succGrab"])) {
//        self.user.succGrab = [userinfo[@"succGrab"] integerValue];
//    }
//    if (isNil(userinfo[@"totalGrab"])) {
//        self.user.totalGrab = [userinfo[@"totalGrab"] integerValue];
//    }
//    if (isNil(userinfo[@"paidPoint"])) {
//        self.user.paidPoint = [userinfo[@"paidPoint"] integerValue];
//    }
//    if (isNil(userinfo[@"givePoint"])) {
//        self.user.givePoint = [userinfo[@"givePoint"] integerValue];
//    }
//    if (isNil(userinfo[@"totalPoint"])) {
//        self.user.totalPoint = [userinfo[@"totalPoint"] integerValue];
//    }
//    if (isNil(userinfo[@"partnerId"])) {
//        self.user.partnerId = [userinfo[@"partnerId"] integerValue];
//    }
//    if (isNil(userinfo[@"ranking"])) {
//        self.user.ranking = [userinfo[@"ranking"] integerValue];
//    }
//    if (isNil(userinfo[@"recmdCode"])) {
//        self.user.recmdCode = userinfo[@"recmdCode"];
//    }
//
//    if (isNil(userinfo[@"email"])) {
//        self.user.email = userinfo[@"email"];
//    }
//
//    if (isNil(userinfo[@"country"])) {
//        self.user.country = userinfo[@"country"];
//    }
//    if (isNil(userinfo[@"province"])) {
//        self.user.province = userinfo[@"province"];
//    }
//    if (isNil(userinfo[@"city"])) {
//        self.user.city = userinfo[@"city"];
//    }
//    if (isNil(userinfo[@"district"])) {
//        self.user.district = userinfo[@"district"];
//    }
//    if (isNil(userinfo[@"detailAddress"])) {
//        self.user.detailAddress = userinfo[@"detailAddress"];
//    }
//    if (isNil(userinfo[@"contactName"])) {
//        self.user.contactName = userinfo[@"contactName"];
//    }
//    if (isNil(userinfo[@"contactPhone"])) {
//        self.user.contactPhone = userinfo[@"contactPhone"];
//    }
//    NSDictionary *dic = [self.user properties];
//    NSLog(@"User information=%@",dic);
//    saveValueByKey(dic, @"user_data");
//}

- (void)setLoginMsg:(NSDictionary *)message {
    if (message == nil) {
        self.user = nil;
        saveValueByKey(nil, @"user_login");
        return;
    }
    
    self.user = self.user ? self.user : [[User alloc] init];
    if (isNil(message[@"phone"])) {
        self.user.phone = message[@"phone"];
    }
    if (isNil(message[@"tid"])) {
        self.user.tid = message[@"tid"];
    }
    if (isNil(message[@"token"])) {
        self.user.token = message[@"token"];
    }
    saveValueByKey(message, @"user_login");
}


- (BOOL)isUserLogin {
    if (self.user != nil && self.user.token != nil && self.user.tid != nil) {
        return YES;
    }
    return NO;
}

- (void)logout {
    [self setLoginMsg:nil];
}

@end
